package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.fi.constant.AparOtherStatus;
import com.cinc.ebc.fi.entity.FiOtherPayment;
import com.cinc.ebc.fi.entity.FiOtherReceive;
import com.cinc.ebc.fi.entity.FiOtherReceiveDetail;
import com.cinc.ebc.fi.mapper.FiOtherReceiveDetailMapper;
import com.cinc.ebc.fi.mapper.FiOtherReceiveMapper;
import com.cinc.ebc.fi.service.IFiOtherReceiveService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 其他应收单
 * @Author: jeecg-boot
 * @Date:   2022-12-25
 * @Version: V1.0
 */
@Service
public class FiOtherReceiveServiceImpl extends ServiceImpl<FiOtherReceiveMapper, FiOtherReceive> implements IFiOtherReceiveService {

	@Autowired
	private FiOtherReceiveMapper fiOtherReceiveMapper;
	@Autowired
	private FiOtherReceiveDetailMapper fiOtherReceiveDetailMapper;

	@Override
	@Transactional
	public void saveMain(FiOtherReceive fiOtherReceive, List<FiOtherReceiveDetail> fiOtherReceiveDetailList) {
		BigDecimal receiveAmount = fiOtherReceive.getReceiveAmount();
		if(receiveAmount == null || receiveAmount.compareTo(BigDecimal.ZERO) != 1){
			throw new RuntimeException("应收金额必须大于零");
		}
		fiOtherReceiveMapper.insert(fiOtherReceive);
		if(fiOtherReceiveDetailList!=null && fiOtherReceiveDetailList.size()>0) {
			for(FiOtherReceiveDetail entity:fiOtherReceiveDetailList) {
				BigDecimal detailReceiveAmount = entity.getReceiveAmount();
				if(detailReceiveAmount == null || detailReceiveAmount.compareTo(BigDecimal.ZERO) != 1){
					throw new RuntimeException("应收金额必须大于零");
				}
				//外键设置
				entity.setMainId(fiOtherReceive.getId());
				fiOtherReceiveDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FiOtherReceive fiOtherReceive,List<FiOtherReceiveDetail> fiOtherReceiveDetailList) {
		FiOtherReceive fiOtherReceiveHis = fiOtherReceiveMapper.selectById(fiOtherReceive.getId());
		if(!AparOtherStatus.STATUS_01.equals(fiOtherReceiveHis.getStatus())){
			throw new RuntimeException("已经审核通过，不能修改");
		}
		BigDecimal receiveAmount = fiOtherReceive.getReceiveAmount();
		if(receiveAmount == null || receiveAmount.compareTo(BigDecimal.ZERO) != 1){
			throw new RuntimeException("应收金额必须大于零");
		}
		fiOtherReceiveMapper.updateById(fiOtherReceive);

		//1.先删除子表数据
		fiOtherReceiveDetailMapper.deleteByMainId(fiOtherReceive.getId());

		//2.子表数据重新插入
		if(fiOtherReceiveDetailList!=null && fiOtherReceiveDetailList.size()>0) {
			for(FiOtherReceiveDetail entity:fiOtherReceiveDetailList) {
				BigDecimal detailReceiveAmount = entity.getReceiveAmount();
				if(detailReceiveAmount == null || detailReceiveAmount.compareTo(BigDecimal.ZERO) != 1){
					throw new RuntimeException("应收金额必须大于零");
				}
				//外键设置
				entity.setMainId(fiOtherReceive.getId());
				fiOtherReceiveDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		FiOtherReceive fiOtherReceiveHis = fiOtherReceiveMapper.selectById(id);
		if(!AparOtherStatus.STATUS_01.equals(fiOtherReceiveHis.getStatus())){
			throw new RuntimeException("已经审核通过，不能删除");
		}
		fiOtherReceiveDetailMapper.deleteByMainId(id);
		fiOtherReceiveMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			delMain(id.toString());
		}
	}

	@Transactional
	@Override
	public void audit(List<FiOtherReceive> fiOtherReceiveList) {
		for(FiOtherReceive fiOtherReceive : fiOtherReceiveList) {
			FiOtherReceive fiOtherReceiveEntity = fiOtherReceiveMapper.selectById(fiOtherReceive.getId());
			if (fiOtherReceiveEntity == null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiOtherReceiveEntity.getStatus();
			if (!AparOtherStatus.STATUS_01.equals(status)) {
				throw new RuntimeException("已经审核，请不要重复操作");
			}
			BigDecimal receiveAmount = fiOtherReceiveEntity.getReceiveAmount();
			if (receiveAmount == null || receiveAmount.compareTo(BigDecimal.ZERO) != 1) {
				throw new RuntimeException("应收金额无效，不能审核");
			}
			// 审核
			FiOtherReceive fiOtherReceiveAudit = new FiOtherReceive();
			fiOtherReceiveAudit.setId(fiOtherReceive.getId());
			fiOtherReceiveAudit.setAuditDate(new Date());
			fiOtherReceiveAudit.setAuditFlag("1");
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiOtherReceiveAudit.setAuditor(sysUser != null ? sysUser.getUsername() : null);
			fiOtherReceiveAudit.setStatus(AparOtherStatus.STATUS_02);
			fiOtherReceiveMapper.updateById(fiOtherReceiveAudit);
		}
	}

}
